rm(list=ls())

library('reshape2')
library('dplyr')
library('matrixStats')
library('ggplot2')

CF <- read.csv("matlab/baseline_model/counterfactuals_prpeace_replication.csv",stringsAsFactors = F)
N <- dim(CF)[1]
cf_labs <- c("All", "U.S.", "U.K.", "France", "Russia", "China")
cv <- 1.96

Mall <- select(CF, starts_with("all")) - matrix(CF$data, nrow=N, ncol=6)
Mall <- as.matrix(Mall)
Mno <- select(CF, starts_with("no")) - matrix(CF$data, nrow=N, ncol=6)
Mno <- as.matrix(Mno)
M <- cbind(Mall, Mno)*100
ggdat <- data.frame(mean.diff = colMeans(M),
                    sd.diff = colSds(M),
                    cf = cf_labs,
                    type = rep(c("Intervene", "Stay out"), each=6))

graphfinal  <- ggplot(ggdat) + facet_grid(rows=vars(type), scales="free") + 
  geom_pointrange(aes(x=factor(cf, levels=cf_labs), 
                      y=mean.diff, ymax=mean.diff+cv*sd.diff, ymin=mean.diff-cv*sd.diff), size=0.9) + 
  theme_bw(18) + ylab("Effect on Probability of Peace") + xlab("Counterfactual") +
  geom_hline(yintercept = 0, linetype="dashed", color="grey45", size=1.25) + 
  theme(axis.title.x=element_text(margin = margin(t = 15, r = 0, b = 0, l = 0))) 

ggsave("fig2.pdf", plot=graphfinal, height=6, width=10.5)
